package com.easy.demo.validation;

import com.basic.exception.BaseException;
import com.easy.demo.entity.User;
import com.easy.demo.mapper.UserMapper;
import com.easy.interfaces.ValidInterface;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 自定义校验 {@link User} 对象
 *
 * @author LZH
 * @version 1.0.11
 * @since 2023/12/31
 */
@Component
public class ValidUser implements ValidInterface {

    @Resource
    private UserMapper userMapper;

    /**
     * 自定义参数校验逻辑
     *
     * @param params {@link User}
     * @param <T>    泛型
     */
    @Override
    public <T> void handle(T... params) {
        User user = (User) params[0];

        // 校验用户是否存在, 若存在则打印
        user = Optional
            .ofNullable(userMapper.selectById(user.getId()))
            .orElseThrow(() -> new BaseException("暂无该用户数据"));
        System.out.println(user);

    }
}
